package tn.softcom.dfbuilder;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;

/**
 * Servlet implementation class UploadFile
 */
public class UploadFile extends HttpServlet {
	/**
	 * 
	 */
	private File savedFile;
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		try {
			Utils.setConnectionParam(request);
			PrintWriter out = response.getWriter();
			DiskFileUpload upload = new DiskFileUpload();
			List items = upload.parseRequest(request);
			System.out.println("items" + items);
			File fullFile = null;
			Iterator it = items.iterator();
			while (it.hasNext()) {
				FileItem item = (FileItem) it.next();
				if (item.isFormField()) {
					request.setAttribute(item.getFieldName(), item.getString());
					System.err.println("ATTR=" + item.getFieldName()
							+ " VALUE=" + item.getString());
				} else if (!item.isFormField() && item.getName() != null
						&& item.getName().length() != 0) {
					fullFile = new File(item.getName());
					savedFile = new File(request.getRealPath("") + "/uploads/",
							fullFile.getName());
					item.write(savedFile);
					System.out.println(savedFile.getAbsolutePath());
					request.getSession().setAttribute("file",
							savedFile.getAbsolutePath());
					request.setAttribute(item.getFieldName(),
							savedFile.getAbsolutePath());
					// ///////////////////////////////////////////////////
					// /////////// DESIGN RESULT //////////////////////
					String techno = request.getParameter("techno");
					System.out.println("techno = " + techno);
					designResult(out);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	private void designResult(PrintWriter out) {
			out.write("<input type=\"hidden\" name=\"url\" value='"
					+ savedFile.getAbsolutePath() + "'>");
			out.write("    <table>");
			 out.write("    <tr>");
		      out.write("        <th style='text-align: left'>");
		      out.write("            Operation ");
		      out.write("        </th>");
		      out.write("        <td>");
		      out.write("            <select name=\"operation\">");
		      out.write("                <option>New</option>");
		      out.write("                <option>Audit</option>");
		      out.write("            </select>");
		      out.write("        </td>");
		      out.write("    </tr>");
			out.write("    <tr>\n");
			out.write("        <td>\n");
			out.write("        </td>\n");
			out.write("        <td>");
			out.write("            <a  style='margin-left: 100%;' class=\"button\" onclick=\"document.getElementById('frm').submit()\">\n");
			out.write("            <span>Generate</span>\n");
			out.write("            </a>\n");
			out.write("        </td>\n");
			out.write("    </tr>\n");
			out.write("</table>");
	}
	
}
